package com.xuan.mail.service.impl;

import com.rabbitmq.client.Channel;
import com.xuan.mail.config.RabbitMqConfig;
import com.xuan.mail.domin.Email;
import com.xuan.mail.mapper.EmailMapper;
import com.xuan.mail.service.EmailService;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

@Service
public class EmailConsumer {
    @Resource
    private EmailService emailService;

    @RabbitListener(queues = RabbitMqConfig.EMAIL_QUEUE)
    public void processEmail(Email email,Message message,Channel channel)throws IOException {
        try {
            //发送邮件
            if (email.getIsdraft()==1){
                emailService.sendEmail(email.getEmailId());
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            System.out.println("接受到的消息为"+new String(message.getBody(),"UTF-8"));
            channel.queuePurge(RabbitMqConfig.EMAIL_QUEUE);
        }catch (Exception e){
            e.printStackTrace();
            channel.queueDelete(RabbitMqConfig.EMAIL_QUEUE);
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);

        }

    }
}
